// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/selective_integer_column_reader.h>

namespace kumo::pollux::parquet {
    class IntegerColumnReader : public dwio::common::SelectiveIntegerColumnReader {
    public:
        IntegerColumnReader(
            const TypePtr &requestedType,
            std::shared_ptr<const dwio::common::TypeWithId> fileType,
            ParquetParams &params,
            common::ScanSpec &scanSpec)
            : SelectiveIntegerColumnReader(
                requestedType,
                params,
                scanSpec,
                std::move(fileType)) {
        }

        bool hasBulkPath() const override {
            return !formatData_->as<ParquetData>().isDeltaBinaryPacked() &&
                   !this->fileType().type()->isLongDecimal() &&
                   ((this->fileType().type()->isShortDecimal())
                        ? formatData_->as<ParquetData>().hasDictionary()
                        : true);
        }

        void seekToRowGroup(int64_t index) override {
            SelectiveIntegerColumnReader::seekToRowGroup(index);
            scanState().clear();
            readOffset_ = 0;
            formatData_->as<ParquetData>().seekToRowGroup(index);
        }

        uint64_t skip(uint64_t numValues) override {
            formatData_->as<ParquetData>().skip(numValues);
            return numValues;
        }

        void getValues(const RowSet &rows, VectorPtr *result) override {
            auto &fileType = static_cast<const ParquetTypeWithId &>(*fileType_);
            auto logicalType = fileType.logicalType_;
            if (logicalType.has_value() && logicalType.value().__isset.INTEGER &&
                !logicalType.value().INTEGER.isSigned) {
                getUnsignedIntValues(rows, requestedType_, result);
            } else {
                getIntValues(rows, requestedType_, result);
            }
        }

        void read(
            int64_t offset,
            const RowSet &rows,
            const uint64_t * /*incomingNulls*/) override {
            POLLUX_WIDTH_DISPATCH(
                parquetSizeOfIntKind(fileType_->type()->kind()),
                prepareRead,
                offset,
                rows,
                nullptr);
            readCommon<IntegerColumnReader, true>(rows);
            readOffset_ += rows.back() + 1;
        }

        template<typename ColumnVisitor>
        void readWithVisitor(const RowSet &rows, ColumnVisitor visitor) {
            formatData_->as<ParquetData>().readWithVisitor(visitor);
        }
    };
} // namespace kumo::pollux::parquet
